﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAWSharp.Common.Helpers
{
    public static class ConversionHelper
    {
        public static float CalulateRMS(List<float> values)
        {
            float sum = 0;
            for (int i = 0; i < values.Count; i++)
            {
                sum += (values[i] * values[i]);
            }
            return (float)Math.Sqrt(sum / values.Count);
        }

        public static float SixteenBitToFloat(Int16 input)
        {
            float min = 32767.0f;
            float max = 32768.0f;
            float sample = 0.0f;

            if (input > 0)
            {
                sample = (float)input / max;
            }
            else
            {
                sample = (float)input / min;
            }

            return sample;
        }

        public static Int16 FloatToSixteenBit(float input)
        {
            float min = 32767.0f;
            float max = 32768.0f;
            Int16 sample = 0;

            if (input > 0)
            {
                sample = (Int16)(input * max);
            }
            else
            {
                sample = (Int16)(input * min);
            }

            return sample;
        }

        public static float TwentyFourBitToFloat(byte[] input)
        {
            int SignedInt24 = 0;
            byte b0 = 0xff;

            if ((input[2] & 0x80) != 0)
                SignedInt24 |= b0 << 24;

            SignedInt24 |= input[2] << 16;
            SignedInt24 |= input[1] << 8;
            SignedInt24 |= input[0];

            float min = 8388607.0f;
            float max = 8388608.0f;
            float sample = 0.0f;

            if (SignedInt24 > 0)
            {
                sample = (float)SignedInt24 / max;
            }
            else
            {
                sample = (float)SignedInt24 / min;
            }

            return sample;
        }

        public static byte[] FloatToTwentyFourBit(float input)
        {
            float min = 8388607.0f;
            float max = 8388608.0f;
            int sample = 0;

            if (input > 0)
            {
                sample = (int)(input * max);
            }
            else
            {
                sample = (int)(input * min);
            }

            byte[] bytes = BitConverter.GetBytes(sample);
            return new byte[] { bytes[0], bytes[1], bytes[2] };
        }

        public static float ThirtyTwoBitToFloat(Int32 input)
        {
            float min = 2147483647.0f;
            float max = 2147483648.0f;
            float sample = 0.0f;

            if (input > 0)
            {
                sample = (float)input / max;
            }
            else
            {
                sample = (float)input / min;
            }

            return sample;
        }

        public static Int32 FloatToThirtyTwoBit(float input)
        {
            float min = 2147483647.0f;
            float max = 2147483648.0f;
            Int32 sample = 0;

            if (input > 0)
            {
                sample = (Int32)(input * max);
            }
            else
            {
                sample = (Int32)(input * min);
            }

            return sample;
        }
    }
}
